package com.multicampus.simplewiki.parser.replacer;

import org.apache.oro.text.perl.Perl5Util;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

public class CamelCaseReplacer implements Replacer {
	private static Pattern camelcasePattern = null;

	public CamelCaseReplacer() {
		PatternCompiler compiler = new Perl5Compiler();
		try {
			if (camelcasePattern == null)
				camelcasePattern = compiler.compile("[A-Z][a-z]+[A-Z][a-z]+");
		} catch (MalformedPatternException e) {

		}
	}

	public String replace(String contents) {
		String htmlString = null;
		PatternMatcher matcher = new Perl5Matcher();
		Perl5Util perl = new Perl5Util();
		String subString = "s/[A-Z][a-z]+[A-Z][a-z]+/" +
				"<a href=\"\\/simplewiki\\/newView_page.do?" +
				"parentId=\\${parentId}&title=$0\">$0<\\/a>/g";
		
		if(perl.match("/[A-Z][a-z]+[A-Z][a-z]+/", contents)){
			htmlString = perl.substitute(subString, contents);
		}
		else
			htmlString = contents;

		return htmlString;
	}

}
